/*
 * Class: FileCSVHelper
 * Created by: Mary Joy Pascual
 * Date Created: 06/17/2014
 * Company: Netsuite
*/
package com.netsuite.helper;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.netsuite.migration.CSV;
import com.netsuite.migration.CSVContent;

import au.com.bytecode.opencsv.CSVReader;

public class FileReaderCSVHelper {

	public static CSV fileReader(String dir, String fileName) {
		CSV csv = new CSV();
		CSVReader reader = null;
		try {
			reader = new CSVReader(new FileReader(dir+"\\"+fileName), ',');
			String[] nextLine;
			int i = 0;
			List<String> title = new ArrayList<String>();
			List<CSVContent> contents = new ArrayList<CSVContent>();
			while ((nextLine = reader.readNext()) != null) {
				CSVContent content = new CSVContent();
				content.setCsvContents(new ArrayList<String>());
				for(String token : nextLine){
					if(i == 0){
						title.add(token);
					} else {
						content.getCsvContents().add(token);
					}
				}
				if(i > 0) {
					contents.add(content);
				}
				i++;
			}
			csv.setCsvTitle(title);
			csv.setCsvContents(contents);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return csv;
	}
}
